package com.topdt.jbpm.utils;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.thx.common.util.AppContext;




public class DocumentConvertUtil {
	private static final Log logger = LogFactory.getLog(DocumentConvertUtil.class);
	
	public static void txt2Pdf(String srcFileName, String destFileName){
		Document document = new Document(PageSize.A4, 36, 36, 36, 36);
		try {
			PdfWriter.getInstance(document, new FileOutputStream(
					destFileName));
			//
			document.addTitle("file change");
			document.addAuthor("lxyan ");
			document.addSubject("This example explains how to change.");
			document.addKeywords("iText, step 3, metadata");
			document.addCreator("My program using iText");
			//
			document.open();
			PdfPTable table = new PdfPTable(1);
			// BaseFont bfChinese = BaseFont.createFont("STSong-Light",
			// "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

			BaseFont bfChinese = BaseFont.createFont(
					"/opt/apps/fonts/simhei.ttf", BaseFont.IDENTITY_H,
					BaseFont.NOT_EMBEDDED);

			Font FontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.RED);
			// read txt file
			FileReader fr = new FileReader(srcFileName);
			BufferedReader br = new BufferedReader(fr);
			String s;
			while ((s = br.readLine()) != null) {
				Paragraph par = new Paragraph(s, FontChinese);
				document.add(par);
				PdfPCell cell = new PdfPCell();
				cell.addElement(new Paragraph(s, FontChinese));
				table.addCell(cell);
			}

			fr.close();
//			document.add(table);
			//
		} catch (DocumentException de) {
			System.err.println(de.getMessage());
		} catch (IOException ioe) {
			System.err.println(ioe.getMessage());
		}
		document.close();				
	}
	public static void doc2Pdf(String srcFileName, String destFileName) {
//		File inputFile = new File(
//				"C:\\Documents and Settings\\ccnustone\\桌面\\新建文件夹\\数据挖掘技术简介.doc");
//		File outputFile = new File(
//				"C:\\Documents and Settings\\ccnustone\\桌面\\新建文件夹\\数据挖掘技术简介.pdf");
		logger.info("doc2Pdf源文件名"+srcFileName+"目标文件名"+destFileName);
		File inputFile = new File(srcFileName);
		File outputFile = new File(destFileName);
		// connect to an OpenOffice.org instance running on port 8100
		String socketOpenOfficeIp = AppContext.getProperties("SocketOpenOfficeIp");
		String socketOpenOfficePort = AppContext.getProperties("SocketOpenOfficePort");
		OpenOfficeConnection connection = new SocketOpenOfficeConnection(socketOpenOfficeIp,Integer.parseInt(socketOpenOfficePort));
		try {
			connection.connect();
		} catch (ConnectException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// convert
		DocumentConverter converter = new OpenOfficeDocumentConverter(
				connection);
		converter.convert(inputFile, outputFile);

		// close the connection
		connection.disconnect();

	}	
	
	
	/**
	 * 
	 * @author 冯晋首
	 * @date   2015-4-15 下午6:04:18 
	 * @param srcFileName
	 * @param destPath
	 * @return
	 * @throws IOException
	 */
	public static String pdfToPng(String srcFileName, String destPath) throws IOException {
		org.icepdf.core.pobjects.Document document = new org.icepdf.core.pobjects.Document();
		try 
		{
			document.setFile(srcFileName);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		int pages = document.getNumberOfPages();
		BufferedImage image = null;
		RenderedImage rendImage = null;
		String imageNames = "";
		for( int i = 0 ; i < pages ; i++ )
		{
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String nowTimeStr = sDateFormat.format(new Date());
			Random r = new Random();
			int rannum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 10000;
			image = (BufferedImage)document.getPageImage(i,GraphicsRenderingHints.SCREEN,Page.BOUNDARY_TRIMBOX, 0f, 1.5f) ;
			rendImage = image;
			File file = new File(destPath+"//"+ nowTimeStr+rannum + ".png");
			imageNames += nowTimeStr+rannum + ".png"+",";
			try 
			{
				ImageIO.write(rendImage, "png", file);
			} 
			catch (IOException e) 
			{
				ImageIO.write(rendImage, "png", file);
			} 
		}
		document.dispose();
		return imageNames.substring(0, imageNames.lastIndexOf(","));
		
	}
	
	public static void main(String[] args) {
		/*try {
			DocumentConvertUtil.pdf2Jpg("D:/12345.pdf", "D:/saasoa_thx/uploadpdf/filesimage");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
}
